package fr.labri.buganalyzer.eclipse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class EclipseProjectsAnalysis {

	public static final String ECLIPSE_GIT_URL = "http://git.eclipse.org";
	public static final String REPO_TAG = "/c/";
	private static final CharSequence GIT_TAG = "/gitroot/";
	private static final CharSequence OFFSET_TAG = "?ofs=";
	private static final int MAX_OFFSET = 450; //450
	private static final int OFFSET = 50;
	public static final int TIMEOUT = 10000;

	private Map<String, List<String>> repos;

	public EclipseProjectsAnalysis() {
		repos = new HashMap<>();
	}

	public final Map<String, List<String>> getRepos() {
		return repos;
	}

	public final void setRepos(Map<String, List<String>> repos) {
		this.repos = repos;
	}

	public final void downloadReposList() {
		try {
			for (int i = 0; i < MAX_OFFSET; i += OFFSET) {
				Document doc = Jsoup.connect(ECLIPSE_GIT_URL + REPO_TAG + OFFSET_TAG + i).timeout(TIMEOUT).get();
				List<String> urlList = null;
				// top level repos (repositories da
				Elements elements = doc.getElementsByClass("toplevel-repo");
 				for (Element elm : elements) {
 					urlList = new ArrayList<String>();
					String repoName = elm.getElementsByTag("a").attr("title");
					String repoRelativeLink = elm.getElementsByTag("a").attr("href");
					String gitUrl = getGitUrl(repoRelativeLink);
					urlList.add(gitUrl);
					repos.put(repoName, urlList);
				}
 				
 				elements = doc.getElementsByClass("reposection");
 				for (Element elm : elements) {
 					String name = elm.text();
 					Elements subElements = doc.getElementsByClass("sublevel-repo");
 					urlList = new ArrayList<String>();
					for (Element subElm: subElements) {
						if (subElm.getElementsByTag("a").attr("title").contains(name)) {
							String repoRelativeLink = subElm.getElementsByTag("a").attr("href");
							String gitUrl = getGitUrl(repoRelativeLink);
							urlList.add(gitUrl);
							repos.put(name, urlList);
						}
//						String repoName = subElm.getElementsByTag("a").attr("title");
					}
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public final void downloadReposListOld() {
		try {
			for (int i = 0; i < MAX_OFFSET; i += OFFSET) {
				Document doc = Jsoup.connect(ECLIPSE_GIT_URL + REPO_TAG + OFFSET_TAG + i).timeout(TIMEOUT).get();
				Elements element = doc.getElementsByClass("toplevel-repo");
				element.addAll(doc.getElementsByClass("sublevel-repo"));
				for (Element elm : element) {
					String repoName = elm.getElementsByTag("a").attr("title");
					String repoRelativeLink = elm.getElementsByTag("a").attr("href");
					String gitUrl = getGitUrl(repoRelativeLink);
//					repos.put(repoName, gitUrl);
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private String getGitUrl(String repoRelativeLink) {
		String url = ECLIPSE_GIT_URL;
		return url + repoRelativeLink.replace(REPO_TAG, GIT_TAG);
	}
	
	public final void writeToJsonFile() throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
		mapper.writeValue(new File("resources/testData/NewEclipseGitRepos.json"), repos);
	}
	
	public static void main(String[] args) throws IOException {
		EclipseProjectsAnalysis analyzer = new EclipseProjectsAnalysis();
		analyzer.downloadReposList();
		analyzer.writeToJsonFile();
		System.out.println("Got " + analyzer.repos.size() + " git repo");
	}

}
